package v1

import (
	"github.com/gin-gonic/gin"
	"tdy-backend/app/core"
	memberService "tdy-backend/app/member/service"
	"tdy-backend/app/panoramic/dto"
	"tdy-backend/app/panoramic/service"
	"tdy-backend/global"
)

type WorksController struct {
}

// @Summary 作品提交
// @Tags 管理端-全景作品
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.WorksCommitDto true "新增全景作品参数"
// @Router /Works/Commit [post]
func (o *WorksController) Commit(c *gin.Context) {
	var err error
	var param dto.WorksCommitDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, "参数缺失")
		return
	}
	db := global.TYD_DB.Begin()
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}

	worksService := service.WorksService{}
	worksService.MakeORM(db).MakeMemberInfo(memberInfo)
	_, err = worksService.Add(param)
	if err != nil {
		db.Rollback()
		core.CustomError(c, err.Error())
		return
	}
	db.Commit()
	core.Ok(c, nil, "创建成功")
}

// @Summary 作品批量删除
// @Tags 管理端-全景作品
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.WorksDeleteDto true "批量删除全景作品参数"
// @Router /Works/Delete [post]
func (o *WorksController) Delete(c *gin.Context) {
	var err error
	var param dto.WorksDeleteDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, "参数缺失")
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	worksService := service.WorksService{}
	worksService.MakeORM(db).MakeMemberInfo(memberInfo)
	_, err = worksService.Delete(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "删除成功")
}

// @Summary 作品分页查询
// @Tags 管理端-全景作品
// @Param       token	header string true "登录标识"
// @Param	Page	query	uint false "页码"
// @Param	PageSize	query	uint false "页尺寸"
// @Param	Keyword	query	string false "搜索关键词"
// @Param	Status	query	int false "状态 1启用 2禁用"
// @Router /Works/ListForPage [get]
func (o *WorksController) ListForPage(c *gin.Context) {
	param := dto.WorksListDto{}
	c.Bind(&param)
	token := c.Request.Header.Get("token")
	db := global.TYD_DB
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	worksService := service.WorksService{}
	worksService.MakeORM(db).MakeMemberInfo(memberInfo)
	data := worksService.ListForPage(param)
	core.Ok(c, data, "请求成功")
}

// @Summary 作品编辑
// @Tags 管理端-全景作品
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.WorksEditDto true "编辑全景作品参数"
// @Router /Works/Edit [post]
func (o *WorksController) Edit(c *gin.Context) {
	var err error
	var param dto.WorksEditDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, "参数缺失")
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	worksService := service.WorksService{}
	worksService.MakeORM(db).MakeMemberInfo(memberInfo)
	_, err = worksService.Edit(param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "编辑成功")
}
